package com.saltedfish.tool.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import com.saltedfish.tool.R;
import butterknife.BindView;

import cn.droidlover.xdroidmvp.popwindow.DialogHelper;

public class PolicyAgreeDialog extends DialogHelper {

    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_center_title)
    TextView tvCenterTitle;
    @BindView(R.id.tv_desc)
    TextView tvDesc;

    private Context context;

    public PolicyAgreeDialog(Activity context) {
        super(context, R.style.AlertDialogStyle);
        this.context = context;
        setFullWidthScreen();
        setGravity(Gravity.CENTER);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_agree_policy;
    }

    @Override
    protected void initView() {
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener != null){
                    listener.onClick("1");
                }
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener != null){
                    listener.onClick("2");
                }
                dismiss();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void dialogDismiss() {

    }

    public void setDesc(){
        String str = "您可以通过阅读《用户协议》和《隐私政策》了解详细信息";

        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(str);
        //第一个出现的位置
        final int start = str.indexOf("《");

        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                //用户服务协议点击事件
                if(listener != null){
                    listener.onClick("userService");
                }
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                //设置文件颜色
                ds.setColor(Color.parseColor("#3097FF"));
                // 去掉下划线
                ds.setUnderlineText(false);
            }

        }, start, start + 5, 0);
        //最后一个出现的位置
        final int end = str.lastIndexOf("《");
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                //隐私协议点击事件
                if(listener != null){
                    listener.onClick("userSecret");
                }
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                //设置文件颜色
                ds.setColor(Color.parseColor("#3097FF"));
                // 去掉下划线
                ds.setUnderlineText(false);
            }

        }, end, end + 6, 0);
        tvDesc.setMovementMethod(LinkMovementMethod.getInstance());
        tvDesc.setText(ssb, TextView.BufferType.SPANNABLE);
        tvDesc.setHighlightColor(Color.TRANSPARENT); //设置点击后的颜色为透明
        tvDesc.setVisibility(View.VISIBLE);

    }

    public void setTitleHint(String title){
        tvCenterTitle.setText(title);
    }

    public void setActionStrAndColor(String left, int colorLeft, String right, int colorRight){
        tvCancel.setText(right);
        tvCancel.setTextColor(colorRight);
        tvConfirm.setTextColor(colorLeft);
        tvConfirm.setText(left);
    }


}
